package site.wenneng.dev.controller;


import com.ruoyi.common.annotation.Anonymous;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import site.wenneng.dev.service.TaskService;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.nio.charset.Charset;

@SuppressWarnings("SpellCheckingInspection")
@RestController
public class HeppmProxyController {

    @Autowired
    private TaskService taskService;

    @Anonymous
    @RequestMapping("/heppm/**")
    public String proxy(HttpServletRequest req) throws IOException {
        String requestURI = req.getRequestURI().replace("/heppm/", "");
        String body = StreamUtils.copyToString(req.getInputStream(), Charset.defaultCharset());
        Request request = new Request.Builder()
                .url("https://dev.hundsun.com/heppm/" + requestURI)
                .post(RequestBody.create(body, MediaType.parse("application/json")))
                .build();
        return taskService.invoke(request);
    }

    @Anonymous
    @RequestMapping("/cas/hs2024V1/bg.dat")
    public String proxyCas(HttpServletRequest req) throws IOException {
        String requestURI = req.getRequestURI().replace("/cas/", "");
        String body = StreamUtils.copyToString(req.getInputStream(), Charset.defaultCharset());
        Request request = new Request.Builder()
                .url("https://hs-cas.hundsun.com/cas/" + requestURI)
                .post(RequestBody.create(body, MediaType.parse("application/json")))
                .build();
        return taskService.invoke(request);
    }
}
